<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="dossierList" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户ID" align="center" key="id" prop="id"  />
      <el-table-column label="档案名称" align="center" key="surname" prop="surname"   />
      <el-table-column label="性别" align="center" key="sex" prop="sex">
        <template slot-scope="scope">
          <span style="color: #00afff;" v-if="scope.row.sex==1">男</span>
          <span style="color: #00afff;" v-if="scope.row.sex==2">女</span>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" key="birthday" prop="birthday"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['kenuo:lease:edit']">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getClientList"
    />
    <!-- 添加或修改档案管理的对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open"  width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="档案名称" prop="surname">
          <el-input v-model="form.surname" placeholder="请输入档案名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-select
            v-model="form.sex"
            placeholder="用户状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in sexs"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDossier,getDossier,addDossier,updateDossier,delDossier} from "@/api/kenuo/dossier";

export default {
  name: "dossier",
  data() {
    return {
      sexs:[
        {
          label:'男',
          value:1
        },
        {
          label:'女',
          value:2
        }
      ],
      username:null,
      dossierList:[],
      loading: true,
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      title:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined
      },
      form:{},
      // 表单校验
      rules: {
        surname: [{
          required: true,
          message: "名称为空",
          trigger: "blur"
        }],
        sex: [{
          required: true,
          message: "请选择性别",
          trigger: "blur"
        }],
        birthday: [{
          required: true,
          message: "请选择出生日期",
          trigger: "blur"
        }]
      },
    }
  },
  created() {
    this.username =this.$route.query.userName;
    this.getClientList();
  },
  methods: {
      getClientList(){
        this.dossierList =[];
        this.loading = true;
        listDossier({
          remarkOne:this.username
        }).then(response => {
          this.dossierList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
      },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$modal.confirm('确认删除吗？').then(function() {
        return delDossier(Ids);
      }).then(() => {
        this.getClientList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDossier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDossier(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getClientList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        surname: null,
        sex: null,
        createTime: null,
        updateTime: null,
        birthday:null
      };
      this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
